<?php
/**
 * Authentication plugin
 *
 * @author Lukas Normantas
 */
class Plugin_Login extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
      
        $auth = Zend_Auth::getInstance();
        $view = Zend_Layout::getMvcInstance()->getView();

        // make login form accessible in views
        $view->loginForm = $loginForm;

        // If no user loged in creates a new user with guest role
        if ( ! $auth->hasIdentity()){
            $auth->authenticate( new Areal_Auth_Identity_Guest() );
        }

        // make identity accessible in views
        $view->identity = $auth->getIdentity();
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {

        $auth = Zend_Auth::getInstance();

        $module = $this->getRequest()->getModuleName();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();

        $do = $module.'/'.$controller.'/'.$action;
//echo $do;
        if (in_array($do, array(
            'content/menu/index',
            'default/index/intro',
            'content/menu/intro',
            'content/album/image',
            'default/user/admin-login',
            'default/error/error',
            'content/album/index','content/album/thumb','default/install/index',
        )) || $auth->getIdentity()->getRoleId() == 'admin'){
            
        } else {

            $request->setDispatched(true);
            $errorHandler = Zend_Controller_Front::getInstance()->getPlugin('Zend_Controller_Plugin_ErrorHandler');
            $this->getRequest()->setModuleName('default');
            $this->getRequest()->setControllerName('index');
            $this->getRequest()->setActionName('intro');


        }

    }

}